package main

import (
	"fmt"
	"sync"
	"time"
	"unsafe"

	"github.com/lxn/win"
)

var porfMap map[string][]int

var m *sync.RWMutex

func init() {
	porfMap = make(map[string][]int)
	porfMap["战士"] = []int{97, 98}
	porfMap["法师"] = []int{97, 98}
	porfMap["魔剑士"] = []int{97, 98, 99}
	porfMap["圣导师"] = []int{97, 98, 99}
	m = new(sync.RWMutex)
}

//技能虚拟按钮
func (this *MyMainWindow) porfVmKey(d time.Duration, value string) {
	porfs := porfMap[value]
	s := d / time.Duration(len(porfs))
	for i := 0; i < len(porfs); i++ {
		vmKeyDown(porfs[i])
		sleep(s - 1)
	}
}

//加血虚拟按键
func (this *MyMainWindow) bloodVmKey() {
	vmKeyDown(81)
	sleep(time.Millisecond)
	vmKeyDown(87)
	sleep(time.Millisecond)
	vmKeyDown(69)
	sleep(time.Millisecond)
	vmKeyDown(82)
	sleep(time.Millisecond)
}

//广告发言虚拟按键
func (this *MyMainWindow) speakVmKey() {
	vmKeyDown(13)
	sleep(time.Millisecond)
	//speak := this.ui.speakLineEdit.Text()
	//fmt.Println(speak)

	win.KeyBDEvent(17, 0, 0, 0)
	win.KeyBDEvent(86, 0, win.KEYEVENTF_KEYUP, 0)
	win.KeyBDEvent(86, 0, 0, 0)
	win.KeyBDEvent(17, 0, win.KEYEVENTF_KEYUP, 0)
	sleep(time.Millisecond)

	vmKeyDown(13)
	sleep(time.Millisecond)
}

//虚拟按键
func vmKeyDown(key int) {
	m.Lock()
	win.KeyBDEvent(key, 0, 0, 0)
	win.KeyBDEvent(key, 0, win.KEYEVENTF_KEYUP, 0)
	m.Unlock()
}

func winForm() {
	cursorPOINT := win.POINT{}
	if win.GetCursorPos(&cursorPOINT) {
		hwndPointNow := win.WindowFromPoint(cursorPOINT)
		if unsafe.Pointer(hwndPointNow) != nil {
			title := make([]byte, 50)
			b := win.GetWindowText(hwndPointNow, &title, int32(len(title)))
			fmt.Println("----- ", cursorPOINT, b, unsafe.Pointer(hwndPointNow))
		}
	}
}
